<?php
class DkpController extends Zend_Controller_Action
{	
	

	
	function indexAction()
	{
		
	}
	
	function standingsAction(){
		$dkpStandingsTable = new DkpStandings();
		$standingsRowset = $dkpStandingsTable->fetchAll();
		
		$result = array();
		
		foreach ($standingsRowset as $sta){
			$result[] = array(	'id'				=> $sta->id,
								'user_id'			=> $sta->user_id,
								'user_name'			=> $sta->findParentRow('Users','Users')->nickname,
								'earned'			=> $sta->earned,
								'spent'				=> $sta->spent,
								'buy_power'			=> $sta->buy_power								
						);
		}
		
		$this->view->standings = $result;		
	}
	
	function evaluateBiddersAction(){
		
		$this->_helper->viewRenderer->setNoRender();
		$characterIds = $this->_request->getParam('characterIds');
		
		$usersCharacters = array();
		
		foreach ($characterIds as $id){
			
			$char = Characters::getById($id);
			if (!$char){
				echo $this->_helper->jsonError("CharacterId");
            	return;
			}			
			$usersCharacters[$char->user_id] = $char;
		}
		
		$standingsTable = new DkpStandings();
		$select = $standingsTable->select();
		
		foreach($usersCharacters as $userId => $char){
			$select->orWhere('user_id = ?',$userId);
		}
		
		$select->order('buy_power DESC');
		
		$rankedUsers = $standingsTable->fetchAll($select);
		
		
		$winner = array(	'character_id' 	=> $usersCharacters[$rankedUsers->current()->user_id]->id,
							'buy_power'		=> $rankedUsers->current()->buy_power,
							'character_name' 	=> $usersCharacters[$rankedUsers->current()->user_id]->name,		
		);
		
		$runnerUps = array();
		
		foreach ($rankedUsers as $rUser){

				$runnerUps[] = array(	'character_id' 	=> $usersCharacters[$rUser->user_id]->id,
										'buy_power'		=> $rUser->buy_power,
										'character_name'=> $usersCharacters[$rUser->user_id]->name,				
				);			
		}
		
		$tie;
		if (count($runnerUps) > 1 && $runnerUps[0]['buy_power'] == $runnerUps[1]['buy_power']){
			$tie = true;
		} else $tie = false;
		
		$result = array(
			'winner'	=> $winner,
			'runner_ups'=> $runnerUps,
			'tie'		=> $tie,
		);
		echo $this->_helper->jsonResponse($result);
		
	}
	
	
    function getCharactersBuyPowerAction(){
		
		$this->_helper->viewRenderer->setNoRender();
		$characterIds = $this->_request->getParam('characterIds');
		
		$usersCharacters = array();
		
		foreach ($characterIds as $id){
			
			$char = Characters::getById($id);
			if (!$char){
				echo $this->_helper->jsonError("CharacterId");
            	return;
			}			
			$usersCharacters[$char->user_id] = $char;
		}
		
		$standingsTable = new DkpStandings();
		$select = $standingsTable->select();
		
		foreach($usersCharacters as $userId => $char){
			$select->orWhere('user_id = ?',$userId);
		}
		
		$select->order('buy_power DESC');
		
		$rankedUsers = $standingsTable->fetchAll($select);
		
		
		$result = array();
		
		foreach ($rankedUsers as $rUser){

				$result[] = array(	'character_name'=> $usersCharacters[$rUser->user_id]->name,
									'character_id' 	=> $usersCharacters[$rUser->user_id]->id,
									'buy_power'		=> $rUser->buy_power,
												
				);			
		}
		
		usort($result,'__sortCharacterName');
		echo $this->_helper->jsonResponse($result);
		
	}
	
	
	

	
	/*
	private function createNormalizationData($userId,$time,$oldEarned,$oldSpent){
		
		$factor = DkpNormalizations::$NORMALIZATION_FACTOR;
		
		$data = array(	'userId' 		=> $userId,
						'time'			=> $time,
						'old_earned'	=> $oldEarned,
						'old_spent'		=> $oldSpent,
						'earned'		=> $oldEarned * $factor,
						'spent'			=> $oldSpent * $factor,
						'factor'		=> $factor
		);
		
		return $data;
		
	}
	
*/
	
}

function __sortCharacterName($a, $b) 
{ 
    return strcasecmp($a['character_name'], $b['character_name']); 
}